package com.youhp.spire.xls.demo.Workbook;

import com.spire.xls.*;
import java.util.*;

public class MergeExcelFiles {
    public static void main(String[] args) {
        String inputFile1 = "data/MergeExcelFiles-1.xlsx";
        String inputFile2 = "data/MergeExcelFiles-2.xls";
        String inputFile3 = "data/MergeExcelFiles-3.xlsx";
        String outputFile = "output/MergeExcelFiles_out.xlsx";

        List<String> files = new ArrayList<String>();
        files.add(inputFile1);
        files.add(inputFile2);
        files.add(inputFile3);

        //Create a new workbook
        Workbook newbook = new Workbook();
        newbook.setVersion(ExcelVersion.Version2010);

        //Clear all worksheets
        newbook.getWorksheets().clear();

        //Create a workbook
        Workbook tempbook = new Workbook();

        for (String file : files)
        {
            //Load the file
            tempbook.loadFromFile(file);
            for (Worksheet sheet : (Iterable<Worksheet>)tempbook.getWorksheets())
            {
                //Copy sheet in a workbook
                newbook.getWorksheets().addCopy(sheet, WorksheetCopyType.CopyAll);
            }
        }

        //Save the file
        newbook.saveToFile(outputFile, ExcelVersion.Version2010);
    }
}
